package cn.iocoder.yudao.module.security.controller.admin.dashboard;

import cn.hutool.core.io.FileUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.module.security.controller.admin.dashboard.vo.*;
import cn.iocoder.yudao.module.security.service.dashboard.DashboardService;
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 大屏")
@RestController
@RequestMapping("/security/dashboard")
public class DashboardController {

    @Resource
    private DashboardService dashboardService;

    @Value("${ppt.filepath}")
    private String tuopu_filePath;

    @GetMapping("/network")
    @Operation(summary = "网络会话")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<List<SecurityDashboardNetworkRespVO>> getNetwork(@Valid SecurityDashboardReqVO reqVO) {
        List<SecurityDashboardNetworkRespVO> result = dashboardService.getNetwork(reqVO);
        return success(result);
    }

    @GetMapping("/protocol-app")
    @Operation(summary = "应用协议分布")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<List<SecurityDashboardProtocolRespVO>> getProtocolApp(@Valid SecurityDashboardReqVO reqVO) {
        List<SecurityDashboardProtocolRespVO> result = dashboardService.getProtocolApp(reqVO);
        return success(result);
    }

    @GetMapping("/protocol-type")
    @Operation(summary = "协议类型分布")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<List<SecurityDashboardProtocolRespVO>> getProtocolType(@Valid SecurityDashboardReqVO reqVO) {
        List<SecurityDashboardProtocolRespVO> result = dashboardService.getProtocolType(reqVO);
        return success(result);
    }

    @GetMapping("/network-event-type")
    @Operation(summary = "关键事件分布")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<List<SecurityDashboardProtocolRespVO>> getNetworkEventType(@Valid SecurityDashboardReqVO reqVO) {
        List<SecurityDashboardProtocolRespVO> result = dashboardService.getNetworkEventType(reqVO);
        return success(result);
    }

    @GetMapping("/protocol-bar")
    @Operation(summary = "协议分布趋势")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<List<SecurityDashboardProtocolBarRespVO>> getProtocolBar(@Valid SecurityDashboardReqVO reqVO) {
        List<SecurityDashboardProtocolBarRespVO> result = dashboardService.getProtocolBar(reqVO);
        return success(result);
    }

    @GetMapping("/network-event")
    @Operation(summary = "协议关键事件")
    @PreAuthorize("@ss.hasPermission('security:whitelist-event:query')")
    public CommonResult<List<NetworkEventRespVO>> getNetworkEvent(@Valid SecurityDashboardReqVO reqVO) {
        List<NetworkEventRespVO> result = dashboardService.getNetworkEvent(reqVO);
        return success(result);
    }


    @GetMapping("/tuopu")
    @Operation(summary = "导出 拓扑图")
    @Parameter(name = "deleteFile", description = "是否删除在服务器本地生成的文件", example = "true")
    public void exportTuopu(@RequestParam(defaultValue = "true") Boolean deleteFile,
                            HttpServletResponse response) throws Exception {
        System.out.println(tuopu_filePath);
        doExportFile(deleteFile, response);
    }

    private void doExportFile(Boolean deleteFile,
                              HttpServletResponse response) throws Exception {
//        String docFileName = "d:/中车大屏6.tiff";
        String filePath = tuopu_filePath.replace("pptx", "tiff");
        String pngPath = tuopu_filePath.replace("pptx", "png");
        String downloadFileName = "666.png"; //下载后的文件名
        try {
            //初始化Presentation类的实例

            Presentation ppt = new Presentation();

            //加载一个示例文档

//            ppt.loadFromFile(tuopu_filePath);


            //将文档保存为tiff格式

//            ppt.saveToFile(filePath, FileFormat.TIFF);

//            BufferedImage tiffImage = ImageIO.read(new File(filePath));
//            ImageIO.write(tiffImage, "PNG", new File(pngPath));
            System.out.println("输出png目录：" + pngPath);
            // 读取，返回
//            ServletUtils.writeAttachment(response, "preview.png", FileUtil.readBytes("d:/preview.png"));
            ServletUtils.writeAttachment(response, downloadFileName, FileUtil.readBytes(pngPath));
        } finally {
            handleDeleteFile(deleteFile, filePath);
        }
    }

    private void handleDeleteFile(Boolean deleteFile, String filePath) {
        if (!deleteFile) {
            return;
        }
        FileUtil.del(filePath);
    }

}
